<?php

/**
 * Enjaz Tech
 *
 * for purchase via the Internet
 *
 * @package		CodeIgniter
 * @author		Hassan
 * @copyright	Copyright (c) 2012 - 2011,Enjaz Tech, Inc.
 * @link		http://wasety.com
 * @since		Version 1.0
 */
class APP_Controller extends CI_Controller {

    var $pass = array();
    var $user_data;

    function __construct() {
        parent::__construct();

        $admin_logged_in = $this->session->userdata('admin_logged_in');
        if (in_array(ADMIN_FOLDER, get_current_url()) && empty($admin_logged_in)) {
            if (uri_string() != 'admin/dashboard/login') {
                redirect(ADMIN_FOLDER . '/dashboard/login');
            }
        }

        $this->main_lang();
    }


    function login_done($type = FALSE, $back_url = '', $mesage = false) {
        if (!in_array($type, array('admin', 'user'))) {
            return false;
        }
        $data_session[$type . '_ID'] = $this->user_data->id;
        $data_session[$type . '_NAME'] = $this->user_data->username;
        $data_session[$type . '_EMAIL'] = $this->user_data->email;
        $data_session[$type . '_PASSWORD'] = $this->user_data->password;
        $data_session[$type . '_MODE'] = $this->user_data->mode;
        $data_session[$type . '_CONFIRM'] = $this->user_data->confirm_email;
        $data_session[$type . '_ACTIVE'] = $this->user_data->active;
        if ($this->user_data->confirm_code)
            $data_session[$type . '_CODE'] = $this->user_data->confirm_code;
        $data_session[$type . '_LASTLOGIN'] = $this->user_data->last_login;
        $data_session[$type . '_ROLES'] = $this->user_data->roles;
        $data_session[$type . '_logged_in'] = true;
        if (!in_array(ADMIN_FOLDER, get_current_url())) {
            $this->$type->update_user_login($this->user_data->id);
        }
        $this->session->set_userdata($data_session);
        if (!$mesage)
            $mesage = "message_login_success";
        $this->themes->set_message(array('url' => $back_url, 'type' => 'success', 'message' => $mesage));
    }

    function destroy_session($type = false) {
        if (!in_array($type, array('admin', 'user'))) {
            return false;
        }
        $data_session[$type . '_ID'] = '';
        $data_session[$type . '_NAME'] = '';
        $data_session[$type . '_EMAIL'] = '';
        $data_session[$type . '_PASSWORD'] = '';
        $data_session[$type . '_CODE'] = '';
        $data_session[$type . '_LASTLOGIN'] = '';
        $data_session[$type . '_CONFIRM'] = '';
        $data_session[$type . '_ACTIVE'] = '';
        $data_session[$type . '_logged_in'] = FALSE;
        $this->session->set_userdata($data_session);
        if ($type == 'admin') {
            $this->session->sess_destroy();
            $url = 'dashboard/login';
            $this->session->sess_create();
        } else {
            $url = '';
        }

        $this->themes->set_message(array('url' => $url, 'type' => 'success', 'message' => 'message_logout_success'));
    }

    function main_lang() {
        if (!in_array(ADMIN_FOLDER, get_current_url())) {
            $lang = false;
            if ($this->session->userdata('def_lang')) {
                $lang = $this->session->userdata('def_lang');
            } else {
                $this->db->select('code');
                $lang = $this->db->get_where('languages', array('default' => 'YES'))->row()->code;
            }
            define('LANG', $lang);

            $autoloadlang = array('message', 'label', 'report');
            if (LANG == "ar") {
                $this->config->set_item('language', 'arabic');
                foreach ($autoloadlang as $langload)
                    $this->lang->load($langload);
            } elseif (LANG == "en") {
                $this->config->set_item('language', 'english');
                foreach ($autoloadlang as $langload)
                    $this->lang->load($langload);
            }
        } else {
            $autoloadlang = array('message', 'label', 'report');
            $this->config->set_item('language', 'arabic');
            foreach ($autoloadlang as $langload)
                $this->lang->load($langload);
        }
    }

}